package com.jlz.actions.console;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Standard;
import org.springframework.beans.factory.annotation.Autowired;

import com.jlz.beans.user.AdminBean;
import com.jlz.managers.user.Admins;
import com.jlz.managers.user.Links;
import com.julewa.Client;
import com.julewa.db.BeanValidator;

/*
 * 注意,console只有唯一的登陆入口: login/admin
 * 初始超级管理员就直接在数据库里创建好了.
 * 
 */
//用更自然的方式使用rest
@Standard(rest="admin.ID")
public class IndexAction {
	private @Autowired Client client;
	private @Autowired BeanValidator validator;
	private @Autowired Admins admins;
	private @Autowired AdminBean admin;
	private String password;
	private String newpassword;
	private String repassword;
	
	public String execute(){
		return "home";
	}
	
	
	//把个人相关的处理统一放到personal里好了,
	//这样清晰点:)
	@Action("displayinfo")
	public String displayInfo(){
		admin = (AdminBean) client.getUser();
		return "edit";
	}	
	
	@Action("modifyinfo")
	public String modifyInfo(){
		admin.setState(Links.State.USING.getValue());
		admins.save(admin);
		client.setUser(admin);
		return "home";
	}	
	
	@Action("displaypass")
	public String displayPass(){
		admin = (AdminBean) client.getUser();
		return "editPwd";
	}
	
	@Action("modifypass")
	public String modifyPass(){
		admin = (AdminBean) client.getUser();
		if(!admin.getPassword().equals(password)){
			client.setError("password","输入密码错误！");
		}
		if(validator.checkField("newpassword", "新密码", newpassword, 6, 60)){
			if(!newpassword.equals(repassword)){
				client.setError("repassword", "两次输入的新密码不一致");
			}
		}
		if (client.hasError()) {
			client.putError("修改密码失败！");
			return "editPwd";
		}
		admin.setPassword(newpassword);
		admins.save(admin);
		client.putMessage("修改密码成功！");
		
		return "home";
	}	
	
	@Action("login")
	public String login(){
		return "login";
	}

	public AdminBean getAdmin() {
		return admin;
	}

	public void setAdmin(AdminBean admin) {
		this.admin = admin;
	}

	public String getNewpassword() {
		return newpassword;
	}

	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}

	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
